package com.wuzf.navigation_test.ui.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.wuzf.navigation_test.databinding.LayoutHomeImageListBinding;
import com.wuzf.navigation_test.databinding.LayoutHomeVideoListBinding;
import com.wuzf.navigation_test.model.Feed;
import com.wuzf.navigation_test.view.MyImageView;
import com.wuzf.navigation_test.view.MyPlayerView;

public class FeedAdapter extends PagedListAdapter<Feed, FeedAdapter.FeedViewHolder> {

    private Context context;
    private LayoutInflater inflater;

    protected FeedAdapter(Context context) {
        super(new DiffUtil.ItemCallback<Feed>() {
            @Override
            public boolean areItemsTheSame(@NonNull Feed oldItem, @NonNull Feed newItem) {
                return oldItem.id == newItem.id;
            }

            @Override
            public boolean areContentsTheSame(@NonNull Feed oldItem, @NonNull Feed newItem) {
                return oldItem.equals(newItem);
            }
        });
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getItemViewType(int position) {
        Feed feed = getItem(position);
        return feed.itemType;
    }

    @NonNull
    @Override
    public FeedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding viewDataBinding = null;
        // 判断类型，创建不同的layout
        if (viewType == Feed.TYPE_IMAGE_TEXT) {
            viewDataBinding = LayoutHomeImageListBinding.inflate(inflater);
        } else {
            viewDataBinding = LayoutHomeVideoListBinding.inflate(inflater);
        }
        return new FeedViewHolder(viewDataBinding.getRoot(), viewDataBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull FeedViewHolder holder, int position) {
        // 将数据保存到页面中
        holder.bindData(getItem(position));
    }

    class FeedViewHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding vBinding;

        public FeedViewHolder(@NonNull View itemView, ViewDataBinding vBinding) {
            super(itemView);
            this.vBinding = vBinding;
        }

        public void bindData(Feed item) {
            if (vBinding instanceof LayoutHomeImageListBinding) {
                LayoutHomeImageListBinding layoutBinding = (LayoutHomeImageListBinding) vBinding;
                layoutBinding.setFeed(item);
                // 设置图片
                MyImageView imageView = layoutBinding.imageView;
                imageView.bindData(item.width, item.height, 6, item.url);
            } else {
                LayoutHomeVideoListBinding layoutBinding = (LayoutHomeVideoListBinding) vBinding;
                layoutBinding.setFeed(item);
                // 设置视频
                MyPlayerView playerView = layoutBinding.playerView;
                playerView.bindData(item.width, item.height, 6, item.cover);
            }
        }
    }
}
